package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

public class BernoulliRV extends DiscreteRV {
	protected double p;
	
	public BernoulliRV(double p) {
		super();
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		this.p = p;
	}
	
	public BernoulliRV(@NonNull Random r, double p) {
		super(r);
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		this.p = p;
	}

	@Override
	public double getEV() {
		return this.p;
	}

	@Override
	public double getVar() {
		return this.p*(1-this.p);
	}

	@Override
	public double getP(int x) {
		if (x==1)
			return this.p;
		else if (x==0)
			return 1-this.p;
		return 0;
	}

	@Override
	public double calcX(double p) {
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		if (p <= this.p)
			return 1;
		return 0;
	}

	@Override
	public double getF(int x) {
		if (x >= 1)
			return 1;
		else if (x >= 0)
			return 1-this.p;
		return 0;
	}

}
